package com.cee.service.Impl;

import com.cee.common.result.ResultCodeEnum;
import com.cee.dao.ExamMapper;
import com.cee.dto.*;
import com.cee.exception.CeeException;
import com.cee.model.*;
import com.cee.service.*;
import com.cee.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private ExamOneSelectService examOneSelectService;
    @Autowired
    private ExamManySelectService examManySelectService;
    @Autowired
    private ExamJudgeService examJudgeService;
    @Autowired
    private ExamCompletionService examCompletionService;
    @Autowired
    private ExamShortAnswerService examShortAnswerService;


    @Autowired
    private CompletionService completionService;
    @Autowired
    private OneSelectService oneSelectService;
    @Autowired
    private ManySelectService manySelectService;
    @Autowired
    private JudgeService judgeService;
    @Autowired
    private ShortAnswerService shortAnswerService;

    @Autowired
    private OptionOneSelectService optionOneSelectService;
    @Autowired
    private OptionManySelectService optionManySelectService;
    @Autowired
    private OptionJudgeService optionJudgeService;


    @Override
    public List<ExamDTO> getExamList(Exam exam) {
        return examMapper.getExamList(exam);
    }

    @Override
    public ExamDTO getExamById(Integer id) {

        // Exam
        Exam exam = examMapper.selectById(id);

        ExamDTO examDTO = BeanUtil.copy(exam, ExamDTO.class);

        // Completion  List 填空
        List<CompletionDTO> completionDTOS = completionService.getListByExamId(examDTO.getId());
        examDTO.setCompletions(completionDTOS);

        // OneSelect  List  单选+选项
        List<OneSelectDTO> oneSelectDTOS = oneSelectService.getListByExamId(examDTO.getId());
        List<OneSelectDTO> rsOneSelects = new ArrayList<>();

        if (oneSelectDTOS != null){
            for (OneSelectDTO one : oneSelectDTOS){
                List<OptionOneSelect> olist = optionOneSelectService.getListById(one.getId());
                one.setList(olist);
                rsOneSelects.add(one);
            }

            examDTO.setOneSelects(rsOneSelects);
        }

        // Judge  List 判断 + 选项
        List<JudgeDTO> judgeDTOS = judgeService.getListByExamId(examDTO.getId());
        List<JudgeDTO> rsJudges = new ArrayList<>();

        if (judgeDTOS != null){
            for (JudgeDTO judge : judgeDTOS){
                List<OptionJudge> olist = optionJudgeService.getListById(judge.getId());
                judge.setList(olist);

                rsJudges.add(judge);
            }

            examDTO.setJudges(rsJudges);
        }


        // ManySelect  List 多选 + 选项
        List<ManySelectDTO> manySelectDTOS = manySelectService.getListByExamId(examDTO.getId());
        List<ManySelectDTO> rsManySelects = new ArrayList<>();
        if (manySelectDTOS != null){
            for (ManySelectDTO many : manySelectDTOS){
                List<OptionManySelect> olist = optionManySelectService.getListById(many.getId());
                many.setList(olist);
                rsManySelects.add(many);
            }

            examDTO.setManySelects(rsManySelects);
        }

        // ShortAnswer  List 简答

        List<ShortAnswerDTO> shortAnswerDTOS = shortAnswerService.getListByExamId(examDTO.getId());
        examDTO.setShortAnswers(shortAnswerDTOS);

        return examDTO;
    }

    @Override
    public void deleteExamById(Integer id) {

        Exam exam = new Exam();
        exam.setId(id);
        exam.setDelDateTime(new Date());

        log.info("逻辑删除试卷, id={}", id);
        examMapper.updateExamById(exam);

    }

    @Override
    public void saveExam(ExamDTO examDTO) {

        if (StringUtils.isEmpty(examDTO.getExamName())){
            throw new CeeException(ResultCodeEnum.PARAM_ERROR);
        }

        log.info("新增exam开始, 试卷名称={}", examDTO.getExamName());

        log.info("新增exam, examDTO={}", examDTO);
        examMapper.insert(examDTO); // 返回新数据ID
        Integer examId = examDTO.getId();


        // 添加试卷单选题
        if (examDTO.getOneSelectList().size()>0 && examDTO.getOneSelectGrade() != null){
            for (Integer oneSelectId: examDTO.getOneSelectList()){

                ExamOneSelect examOneSelect = new ExamOneSelect();
                examOneSelect.setCeeOneSelectId(oneSelectId);
                examOneSelect.setCeeExamId(examId);
                examOneSelect.setGrade(BigDecimal.valueOf(examDTO.getOneSelectGrade()));

                log.info("新增考试单选题, examOneSelect={}", examOneSelect);
                examOneSelectService.saveExamOneSelect(examOneSelect);
            }
        }

        // 添加试卷多选题
        if (examDTO.getManySelectList().size()>0 && examDTO.getOneSelectGrade() != null){
            for (Integer manySelectId: examDTO.getManySelectList()){

                ExamManySelect examManySelect = new ExamManySelect();
                examManySelect.setCeeManySelectId(manySelectId);
                examManySelect.setCeeExamId(examId);
                examManySelect.setGrade(BigDecimal.valueOf(examDTO.getManySelectGrade()));

                log.info("新增考试多选题, examManySelect={}", examManySelect);
                examManySelectService.saveExamManySelect(examManySelect);
            }
        }

        // 添加试卷判断题
        if (examDTO.getJudgeList().size()>0 && examDTO.getJudgeGrade() != null){
            for (Integer judgeId: examDTO.getJudgeList()){

                ExamJudge examJudge = new ExamJudge();
                examJudge.setCeeJudgeId(judgeId);
                examJudge.setCeeExamId(examId);
                examJudge.setGrade(BigDecimal.valueOf(examDTO.getJudgeGrade()));

                log.info("新增考试判断题, examJudge={}", examJudge);
                examJudgeService.saveExamJudge(examJudge);
            }
        }


        // 添加试卷填空题
        if (examDTO.getCompletionList().size()>0 && examDTO.getCompletionGrade() != null){
            for (Integer completionId: examDTO.getCompletionList()){

                ExamCompletion examCompletion = new ExamCompletion();
                examCompletion.setCeeCompletionId(completionId);
                examCompletion.setCeeExamId(examId);
                examCompletion.setGrade(BigDecimal.valueOf(examDTO.getCompletionGrade()));

                log.info("新增考试填空题, examCompletion={}", examCompletion);
                examCompletionService.saveExamCompletion(examCompletion);
            }
        }


        // 添加试卷简答题
        if (examDTO.getShortAnswerList().size()>0 && examDTO.getShortAnswerGrade() != null){
            for (Integer shortAnswerId: examDTO.getShortAnswerList()){

                ExamShortAnswer examShortAnswer = new ExamShortAnswer();
                examShortAnswer.setCeeShortAnswerId(shortAnswerId);
                examShortAnswer.setCeeExamId(examId);
                examShortAnswer.setGrade(BigDecimal.valueOf(examDTO.getShortAnswerGrade()));

                log.info("新增考试简答题, examShortAnswer={}", examShortAnswer);
                examShortAnswerService.saveExamShortAnswer(examShortAnswer);
            }
        }

        log.info("新增exam结束, 试卷名称={}", examDTO.getExamName());

    }

    @Override
    public Integer countGradeAndUpdate(Integer examId) {
        Integer total = 0;

        List<ExamOneSelect> examOneSelects = examOneSelectService.getListByExamId(examId);
        if (examOneSelects != null){
            for (ExamOneSelect examOneSelect: examOneSelects){
                if (examOneSelect.getGrade() != null){
                    total += examOneSelect.getGrade().intValue();
                }
            }
        }

        List<ExamManySelect> examManySelects  = examManySelectService.getListByExamId(examId);
        if (examManySelects != null){
            for(ExamManySelect examManySelect : examManySelects){
                if (examManySelect.getGrade() != null){
                    total += examManySelect.getGrade().intValue();
                }
            }
        }

        List<ExamJudge> examJudges = examJudgeService.getListByExamId(examId);
        if (examJudges != null){
            for(ExamJudge examJudge : examJudges){
                if (examJudge.getGrade() != null){
                    total += examJudge.getGrade().intValue();
                }
            }
        }


        List<ExamCompletion> examCompletions = examCompletionService.getListByExamId(examId);
        if (examCompletions != null){
            for(ExamCompletion examCompletion : examCompletions){
                if (examCompletion.getGrade() != null){
                    total += examCompletion.getGrade().intValue();
                }
            }
        }

        List<ExamShortAnswer> examShortAnswers = examShortAnswerService.getListByExamId(examId);
        if (examShortAnswers != null){
            for(ExamShortAnswer examShortAnswer : examShortAnswers){
                if (examShortAnswer.getGrade() != null){
                    total += examShortAnswer.getGrade().intValue();
                }
            }
        }


        // 更新总分
        Exam exam = new Exam();
        exam.setId(examId);
        exam.setExamTotal(BigDecimal.valueOf(total));
        updateExamTotal(exam);

        return total;
    }


    @Override
    public void updateExamTotal(Exam exam) {
        log.info("更新总分， exam={}", exam);
        examMapper.updateExamTotalById(exam);
    }

    @Override
    public void checkExamStatus(Integer id) {
       Exam exam = examMapper.selectById(id);

       if (StringUtils.equals("closed", exam.getExamStatus()) ){
           throw new CeeException(ResultCodeEnum.ERROR_902);
       }

    }

}
